/*******************************************************************************
 * Copyright (c) 2009 Kevin Bierhoff and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Kevin Bierhoff - initial API and implementation
 *******************************************************************************/
package com.googlecode.twelfdevtools.editor;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.IRegion;
import org.eclipse.jface.text.ITextViewer;
import org.eclipse.jface.text.contentassist.CompletionProposal;
import org.eclipse.jface.text.contentassist.ICompletionProposal;
import org.eclipse.jface.text.contentassist.IContentAssistProcessor;
import org.eclipse.jface.text.contentassist.IContextInformation;
import org.eclipse.jface.text.contentassist.IContextInformationValidator;

import com.googlecode.twelfdevtools.model.IDeclarationContext;
import com.googlecode.twelfdevtools.model.IElfFile;
import com.googlecode.twelfdevtools.model.ITwelfConstant;
import com.googlecode.twelfdevtools.model.TwelfModel;

/**
 * @author kevin
 *
 */
public class TwelfContentAssist implements IContentAssistProcessor {
	
	private static Comparator<ITwelfConstant> CONSTANT_ORDER =
		new Comparator<ITwelfConstant>() {

			public int compare(ITwelfConstant arg0, ITwelfConstant arg1) {
				return arg0.getId().compareTo(arg1.getId());
			}
		
	};
	
	private final TwelfEditor editor;
	
	public TwelfContentAssist(TwelfEditor editor) {
		this.editor = editor;
	}

	public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer,
			int offset) {
		IElfFile file = editor.getFile();
		IDocument doc = viewer.getDocument();
//		ITwelfDeclaration d = TwelfModel.getModel().findDeclaration(file, doc, offset);
//		if (d == null)
//			return null;
		
		IDeclarationContext ctx = file.getContext(offset);
		IRegion region = TwelfModel.getModel().findTwelfWord(doc, offset);
		try {
			int off = offset;
			int len = 0;
			String prefix = "";
			if (region != null) {
				off = region.getOffset();
				len = region.getLength();
				prefix = doc.get(off, offset >= off ? offset - off : len);
			}
			
			Map<String, ITwelfConstant> matches = new HashMap<String, ITwelfConstant>();
			ctx.collectMatches(matches, prefix);
			List<ITwelfConstant> matchList = new ArrayList<ITwelfConstant>(matches.values());
			Collections.sort(matchList, CONSTANT_ORDER);
			
			// turn matchList into what we have to return
			ICompletionProposal[] result = new ICompletionProposal[matchList.size()];
			int i = 0;
			for (ITwelfConstant m : matchList) {
				result[i++] = new CompletionProposal(m.getId(), off, len, m.getId().length(),
						null, m.getDeclaration(), null, null);
			}
			return result;
		} catch (BadLocationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	public IContextInformation[] computeContextInformation(ITextViewer viewer,
			int offset) {
		// TODO Auto-generated method stub
		return null;
	}

	public char[] getCompletionProposalAutoActivationCharacters() {
		// TODO Auto-generated method stub
		return null;
	}

	public char[] getContextInformationAutoActivationCharacters() {
		// TODO Auto-generated method stub
		return null;
	}

	public IContextInformationValidator getContextInformationValidator() {
		// TODO Auto-generated method stub
		return null;
	}

	public String getErrorMessage() {
		return "Check back soon";
	}

}
